using Minio;
using Minio.DataModel.Args;

namespace DiLu.Expansion.File.Operates.Minio.Tools;

public static class MinioBucketTool
{
    /// <summary>
    ///     检查桶是否存在
    /// </summary>
    /// <param name="minioClient">Minio客户端</param>
    /// <param name="bucketName">桶名称</param>
    public static async Task<bool> CheckBucketAsync(this IMinioClient minioClient, string bucketName)
    {
        //判断桶是否存在
        return await minioClient.BucketExistsAsync(new BucketExistsArgs().WithBucket(bucketName));
    }

    /// <summary>
    ///     创建桶，如果桶存在不创建
    /// </summary>
    /// <param name="minioClient">Minio客户端</param>
    /// <param name="bucketName">桶名称</param>
    public static async Task CreateBucketAsync(this IMinioClient minioClient, string bucketName)
    {
        //判断桶是否存在
        var bucketExists = await CheckBucketAsync(minioClient, bucketName);
        if (!bucketExists)
        {
            await Task.Delay(0);
            await minioClient
                .MakeBucketAsync(new MakeBucketArgs().WithBucket(bucketName));
        }
    }
}